home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / blank / bserver_v14.lha / BServer_v1.4 / Sources.lha / Sources / clients / Balls.c next >
Encoding:
C/C++ Source or Header  |  1995-03-23  |  8.2 KB  |  376 lines

  1. ;/*
  2. sc RESOPT IGNORE=73 DATA=NEAR NMINC UCHAR CONSTLIB STREQ STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE balls.c
  3. Slink from LIB:c.o balls.o to //Clients/Balls LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD NOICONS STRIPDEBUG
  4. delete balls.o
  5. quit
  6.  
  7.  Bouncing Balls 1.3  (Client for BServer)
  8.  
  9.  Copyright © 1994-1995 by Stefano Reksten of 3AM - The Three Amigos!!!
  10.  All rights reserved.
  11.  
  12.  This was reworked as Aristotelis Grammatikakhs asked.
  13. */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/memory.h>
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/intuition.h>
  20. #include <proto/graphics.h>
  21. #include <clib/alib_protos.h>
  22. #include <time.h>
  23.  
  24. #include "/include/client.h"
  25.  
  26. extern UWORD __stdargs RangeRand( ULONG );
  27.  
  28. char *ver = "$VER: Balls 1.2 "__AMIGADATE__;
  29.  
  30. UWORD chip ball_imgdata[116] = {
  31.  0x1F, 0xC000,
  32.  0xE0, 0x3800,
  33.  0x103, 0xC400,
  34.  0x60F, 0xF300,
  35.  0x800, 0x7C80,
  36.  0x1000, 0xE40,
  37.  0x1000, 0x740,
  38.  0x2000, 0x3A0,
  39.  0x4000, 0x1D0,
  40.  0x5000, 0xD0,
  41.  0x5000, 0xF0,
  42.  0xB000, 0xE8,
  43.  0xB000, 0x68,
  44.  0xB000, 0x68,
  45.  0xB800, 0x68,
  46.  0xB800, 0x48,
  47.  0xB800, 0x48,
  48.  0xB800, 0x8,
  49.  0x7800, 0x10,
  50.  0x5C00, 0x10,
  51.  0x5C00, 0x10,
  52.  0x2E00, 0x20,
  53.  0x1700, 0x40,
  54.  0x13C0, 0x40,
  55.  0x9FE, 0x80,
  56.  0x6FF, 0xE300,
  57.  0x13F, 0xC400,
  58.  0xE0, 0x3800,
  59.  0x1F, 0xC000,
  60.  0x1F, 0xC000,
  61.  0xFF, 0xF800,
  62.  0x1FC, 0x3C00,
  63.  0x7F0, 0xF00,
  64.  0xFFF, 0x8380,
  65.  0x1FFF, 0xF1C0,
  66.  0x1FFF, 0xF8C0,
  67.  0x3FFF, 0xFC60,
  68.  0x7FFF, 0xFE30,
  69.  0x7FFF, 0xFF30,
  70.  0x7FFF, 0xFF10,
  71.  0xFFFF, 0xFF18,
  72.  0xFFFF, 0xFF98,
  73.  0xFFFF, 0xFF98,
  74.  0xFFFF, 0xFF98,
  75.  0xFFFF, 0xFFB8,
  76.  0xFFFF, 0xFFB8,
  77.  0xFFFF, 0xFFF8,
  78.  0x7FFF, 0xFFF0,
  79.  0x7FFF, 0xFFF0,
  80.  0x7FFF, 0xFFF0,
  81.  0x3FFF, 0xFFE0,
  82.  0x1FFF, 0xFFC0,
  83.  0x1FFF, 0xFFC0,
  84.  0xFFF, 0xFF80,
  85.  0x7FF, 0xFF00,
  86.  0x1FF, 0xFC00,
  87.  0xFF, 0xF800,
  88.  0x1F, 0xC000
  89.  };
  90.  
  91. UWORD chip ball_mask[58] = {
  92.  0x1F, 0xC000,
  93.  0xFF, 0xF800,
  94.  0x1FF, 0xFC00,
  95.  0x7FF, 0xFF00,
  96.  0xFFF, 0xFF80,
  97.  0x1FFF, 0xFFC0,
  98.  0x1FFF, 0xFFC0,
  99.  0x3FFF, 0xFFE0,
  100.  0x7FFF, 0xFFF0,
  101.  0x7FFF, 0xFFF0,
  102.  0x7FFF, 0xFFF0,
  103.  0xFFFF, 0xFFF8,
  104.  0xFFFF, 0xFFF8,
  105.  0xFFFF, 0xFFF8,
  106.  0xFFFF, 0xFFF8,
  107.  0xFFFF, 0xFFF8,
  108.  0xFFFF, 0xFFF8,
  109.  0xFFFF, 0xFFF8,
  110.  0x7FFF, 0xFFF0,
  111.  0x7FFF, 0xFFF0,
  112.  0x7FFF, 0xFFF0,
  113.  0x3FFF, 0xFFE0,
  114.  0x1FFF, 0xFFC0,
  115.  0x1FFF, 0xFFC0,
  116.  0xFFF, 0xFF80,
  117.  0x7FF, 0xFF00,
  118.  0x1FF, 0xFC00,
  119.  0xFF, 0xF800,
  120.  0x1F, 0xC000
  121.  };
  122.  
  123. struct IntuitionBase *IntuitionBase;
  124. struct GfxBase *GfxBase;
  125.  
  126. struct DisplayIDInformation *dinfo;
  127.  
  128. struct Screen *scr1, *scr2;
  129.  
  130. #define MAXBALLS 8
  131. #define BALLDIM 0x1D
  132. #define HALFDIM 0x0E
  133.  
  134. struct Ball {
  135.     WORD X, Y;
  136.     WORD OldX, OldY;
  137.     WORD VelX;
  138.     WORD VelY;
  139.     WORD Kin, Pot;
  140.     } balls[MAXBALLS];
  141.  
  142. struct BitMap ballbmap;
  143.  
  144. extern ULONG RangeSeed;
  145.  
  146. UWORD swidth, sheight;
  147.  
  148.  
  149. void SetAllBalls( void )
  150. {
  151. register UBYTE n;
  152.  
  153. for ( n = 0; n < MAXBALLS; n++ )
  154.     {
  155.     balls[n].X = RangeRand( swidth - BALLDIM );
  156.     balls[n].Y = RangeRand( sheight - BALLDIM );
  157.     balls[n].OldX = balls[n].OldY = 0;
  158.     balls[n].VelX = RangeRand( 16 ) - 8;
  159.     balls[n].VelY = 0;
  160.     }
  161. }
  162.  
  163.  
  164. void DrawAllBalls( struct Screen *actscr )
  165. {
  166. register int n;
  167. SetRast( &actscr->RastPort, 0 );
  168. for ( n = 0; n < MAXBALLS; n++ )
  169.     BltMaskBitMapRastPort( &ballbmap, 0, 0,
  170.             &actscr->RastPort, balls[n].X, balls[n].Y,
  171.             BALLDIM, BALLDIM, (ABC|ABNC|ANBC), (PLANEPTR)ball_mask );
  172. }
  173.  
  174.  
  175. void MoveAllBalls( struct Screen *actscr, struct Screen *other )
  176. {
  177. register UBYTE n, m;
  178. UWORD temp, thisBallX, thisBallY, otherBallX, otherBallY, diffX, diffY;
  179. BOOL bchg;
  180.  
  181. for ( n = 0; n < MAXBALLS; n++ )
  182.     BltMaskBitMapRastPort( &ballbmap, 0, 0,
  183.             &actscr->RastPort, balls[n].X, balls[n].Y,
  184.             BALLDIM, BALLDIM, (ABC|ABNC|ANBC), (PLANEPTR)ball_mask );
  185. WaitTOF();
  186. ScreenToFront( actscr );
  187. SpritesOff();
  188. for ( n = 0; n < MAXBALLS; n++ )
  189.     RectFill( &other->RastPort, balls[n].OldX, balls[n].OldY, balls[n].OldX + BALLDIM, balls[n].OldY + BALLDIM );
  190.  
  191. for ( n = 0; n < MAXBALLS; n++ )
  192.     {
  193.     balls[n].OldX = balls[n].X;
  194.     balls[n].OldY = balls[n].Y;
  195.  
  196.     /* Nell'ipotesi di urti con le pareti perfettamente elastici ;-) */
  197.  
  198.     if ( (balls[n].VelX >= 0 && ( balls[n].X + balls[n].VelX >= swidth - BALLDIM )) ||
  199.         (balls[n].VelX < 0 && balls[n].X + balls[n].VelX < 0) )
  200.         balls[n].VelX *= -1;
  201.     else
  202.         balls[n].X += balls[n].VelX;
  203.  
  204.     /* Stessa cosa per il pavimento - Ancora piu' incredibile! :-o */
  205.  
  206.     balls[n].VelY += 1;
  207.     if ( (balls[n].VelY >= 0 && ( balls[n].Y + balls[n].VelY >= sheight - BALLDIM )) ||
  208.         (balls[n].VelY < 0 && balls[n].Y + balls[n].VelY < 0) )
  209.         balls[n].VelY *= -1;
  210.     else
  211.         balls[n].Y += balls[n].VelY;
  212.  
  213.     /* Urto perfettamente elastico pure tra le palle !!! =8-O */
  214.     /* ...Seppur con qualche semplificazione (qualche???) ;-) */
  215.     }
  216.  
  217.     do    {
  218.         bchg = FALSE;
  219.         for ( n = 0; n < MAXBALLS; n++ )
  220.             {
  221.             thisBallX = balls[n].X;
  222.             thisBallY = balls[n].Y;
  223.  
  224.             for ( m = ( n ? 0 : 1 ); m < MAXBALLS; m == n - 1 ? m += 2 : m++ )
  225.                 {
  226.                 otherBallX = balls[m].X;
  227.                 otherBallY = balls[m].Y;
  228.  
  229.                 if ( (thisBallX <= otherBallX && otherBallX <= thisBallX + BALLDIM) &&
  230.                     (thisBallY <= otherBallY && otherBallY <= thisBallY + BALLDIM) )
  231.                     {
  232.                     bchg = TRUE;
  233.                     temp = balls[n].VelX;
  234.                     balls[n].VelX = balls[m].VelX;
  235.                     balls[m].VelX = temp;
  236.                     temp = balls[n].VelY;
  237.                     balls[n].VelY = balls[m].VelY;
  238.                     balls[m].VelY = temp;
  239.  
  240.                     diffX = BALLDIM - (otherBallX > thisBallX ? otherBallX - thisBallX : thisBallX - otherBallX);
  241.                     diffY = BALLDIM - (otherBallY > thisBallY ? otherBallY - thisBallY : thisBallY - otherBallY);
  242.  
  243.                     if ( thisBallX < otherBallX )
  244.                         {
  245.                         balls[n].X -= 1 + (diffX/2);
  246.                         if ( balls[n].X < 0 )
  247.                             balls[n].X = 0;
  248.                         balls[m].X += 1 + (diffX/2);
  249.                         if ( balls[m].X >= swidth - BALLDIM )
  250.                             balls[m].X = swidth - BALLDIM - 1;
  251.                         }
  252.                     else
  253.                         {
  254.                         balls[n].X += 1 + (diffX/2);
  255.                         if ( balls[n].X >= swidth - BALLDIM )
  256.                             balls[n].X = swidth - BALLDIM - 1;
  257.                         balls[m].X -= 1 + (diffX/2);
  258.                         if ( balls[m].X < 0 )
  259.                             balls[m].X = 0;
  260.                         }
  261.  
  262.                     if ( thisBallY < otherBallY )
  263.                         {
  264.                         balls[n].Y -= 1 + (diffY/2);
  265.                         if ( balls[n].Y < 0 )
  266.                             balls[n].Y = 0;
  267.                         balls[m].Y += 1 + (diffY/2);
  268.                         if ( balls[m].Y >= sheight - BALLDIM )
  269.                             balls[m].Y = sheight - BALLDIM - 1;
  270.                         }
  271.                     else
  272.                         {
  273.                         balls[n].Y += 1 + (diffY/2);
  274.                         if ( balls[n].Y >= sheight - BALLDIM )
  275.                             balls[n].Y = sheight - BALLDIM - 1;
  276.                         balls[m].Y -= 1 + (diffY/2);
  277.                         if ( balls[m].Y < 0 )
  278.                             balls[m].Y = 0;
  279.                         }
  280.                     }
  281.                 }
  282.             }
  283.         } while( bchg );
  284. }
  285.  
  286.  
  287. void Blank( void )
  288. {
  289. BOOL success = FALSE;
  290. UBYTE activescr = 0;
  291. UBYTE brightness;
  292. struct Rectangle *rect;
  293.  
  294. rect = GETTXTOSCANRECT(dinfo);
  295. swidth = RECTANGLEWIDTH(rect);
  296. sheight = RECTANGLEHEIGHT(rect);
  297. brightness = GETBRIGHTNESS(dinfo);
  298.  
  299. scr1 = scr2 = NULL;
  300.  
  301. if ( (scr1 = OpenScreenTags( NULL,
  302.         SA_Width, swidth,
  303.         SA_Height, sheight,
  304.         SA_Depth, 2,
  305.         SA_Type, CUSTOMSCREEN,
  306.         SA_Quiet, TRUE,
  307.         SA_DisplayID, DISPLAYID(dinfo),
  308.         SA_Overscan, OSCAN_TEXT,
  309.         TAG_END ) ) &&
  310.  (scr2 = OpenScreenTags( NULL,
  311.         SA_Width, swidth,
  312.         SA_Height, sheight,
  313.         SA_Depth, 2,
  314.         SA_Type, CUSTOMSCREEN,
  315.         SA_Quiet, TRUE,
  316.         SA_DisplayID, DISPLAYID(dinfo),
  317.         SA_Overscan, OSCAN_TEXT,
  318.         TAG_END ) ) )
  319.     {
  320.     success = TRUE;
  321.  
  322.     SetRGB4( &scr1->ViewPort, 0, 0, 0, 0 );
  323.     SetRGB4( &scr1->ViewPort, 1, 0, 8*brightness/100, 15*brightness/100 );
  324.     SetRGB4( &scr1->ViewPort, 2, 0, 4*brightness/100, 10*brightness/100 );
  325.     SetRGB4( &scr1->ViewPort, 3, 0, 0, 8*brightness/100 );
  326.     SetRGB4( &scr2->ViewPort, 0, 0, 0, 0 );
  327.     SetRGB4( &scr2->ViewPort, 1, 0, 8*brightness/100, 15*brightness/100 );
  328.     SetRGB4( &scr2->ViewPort, 2, 0, 4*brightness/100, 10*brightness/100 );
  329.     SetRGB4( &scr2->ViewPort, 3, 0, 0, 8*brightness/100 );
  330.  
  331.     SetAllBalls();
  332.     SetAPen( &scr1->RastPort, 0 );
  333.     SetAPen( &scr2->RastPort, 0 );
  334.  
  335.     while( STILL_BLANKING )
  336.         {
  337.         if ( activescr == 1 )
  338.             MoveAllBalls( scr1, scr2 );
  339.         else
  340.             MoveAllBalls( scr2, scr1 );
  341.         activescr ^= 1;
  342.         }
  343.     SpritesOn();
  344.  
  345.     }
  346. if ( scr1 )
  347.     CloseScreen( scr1 );
  348. if ( scr2 )
  349.     CloseScreen( scr2 );
  350. if ( !success )
  351.     SendClientMsg( ACTION_FAILED );
  352. }
  353.  
  354.  
  355. void __main( char *line )
  356. {
  357. if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
  358.     {
  359.     if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
  360.         {
  361.         if ( dinfo = OpenCommunication() )
  362.             {
  363.             InitBitMap( &ballbmap, 2, BALLDIM, BALLDIM );
  364.             ballbmap.Planes[0] = (PLANEPTR)(ball_imgdata);
  365.             ballbmap.Planes[1] = (PLANEPTR)(ball_imgdata + 58);
  366.  
  367.             RangeSeed = time( NULL );
  368.             Blank();
  369.             CloseCommunication( dinfo );
  370.             }
  371.         CloseLibrary( (struct Library *)GfxBase );
  372.         }
  373.     CloseLibrary( (struct Library *)IntuitionBase );
  374.     }
  375. }
  376.